package com.yami.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.CarListDto;
import com.yami.shop.bean.app.dto.SearchCarDto;
import com.yami.shop.bean.app.param.CarListParam;
import com.yami.shop.bean.app.param.CarSubmitParam;
import com.yami.shop.bean.app.param.EditLicensePlateNumberParam;
import com.yami.shop.bean.app.param.ManagementSubmitParam;
import com.yami.shop.bean.model.CarManagement;
import com.yami.shop.bean.model.CarUser;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.CarManagementMapper;
import com.yami.shop.dao.CarUserMapper;
import com.yami.shop.service.CarManagementService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * CarManagementService
 *
 * @Author Administrator
 * @Date 2025/4/10 16:42
 */
@Service
public class CarManagementServiceImpl extends ServiceImpl<CarManagementMapper, CarManagement> implements CarManagementService {

    @Resource
    private CarManagementMapper carManagementMapper;
    @Resource
    private CarUserMapper carUserMapper;
    @Override
    public IPage<CarListDto> carList(PageParam<CarListDto> page, String ridgepole, String roomNumber) {
        page.setRecords(carManagementMapper.carList(new PageAdapter(page), ridgepole, roomNumber));
        page.setTotal(carManagementMapper.countCarList(ridgepole, roomNumber));
        return page;
    }

    @Override
    public List<CarListDto> myCarList(Integer userId) {
        CarUser carUser = carUserMapper.selectById(userId);
        if(carUser.getStatus() != 1){
            throw new YamiShopBindException("您还没通过业主认证");
        }
        Integer roomId = carUser.getRoomId();
        List<CarListDto> list = carManagementMapper.getMyCarList(roomId);
        return list;
    }

    @Override
    public void submit(CarSubmitParam carSubmitParam) {
        CarUser carUser = carUserMapper.selectById(carSubmitParam.getUserId());
        if(carUser.getStatus() != 1){
            throw new YamiShopBindException("您还没通过业主认证");
        }
        int count = carManagementMapper.getCarCount(carUser.getRoomId());
        if(count >= 3){
            throw new YamiShopBindException("一个业主最多录入三辆车");
        }
        int carCount = carManagementMapper.getLicensePlateNumberCount(carSubmitParam.getLicensePlateNumber());
        if(carCount > 0){
            throw new YamiShopBindException("该车牌号已录入，请不要重复录入");
        }
        CarManagement carManagement = new CarManagement();
        carManagement.setRidgepole(carUser.getRidgepole());
        carManagement.setRoomNumber(carUser.getRoomNumber());
        carManagement.setRidRoom(carUser.getRoomId());
        carManagement.setLicensePlateNumber(carSubmitParam.getLicensePlateNumber());
        carManagementMapper.insert(carManagement);
    }

    @Override
    public SearchCarDto search(String licensePlateNumber) {
        return carManagementMapper.getCarByLicensePlateNumber(licensePlateNumber);
    }

    @Override
    public void managementSubmit(ManagementSubmitParam param, Integer userId) {
        CarUser carUser = carUserMapper.selectById(userId);
        if(carUser.getSuperAdmin() != 1){
            throw new YamiShopBindException("您没有权限");
        }
        String ridgepole = param.getRidgepole();
        Integer roomNumber = Integer.valueOf(param.getRoomNumber());
        String formattedNumber = String.format("%04d", roomNumber);
        Integer roomId = Integer.valueOf(ridgepole + formattedNumber);
        String licensePlateNumber = param.getLicensePlateNumber();
        int carCount = carManagementMapper.getLicensePlateNumberCount(licensePlateNumber);
        if(carCount > 0){
            throw new YamiShopBindException("该车牌号已录入，请不要重复录入");
        }
        CarManagement carManagement = new CarManagement();
        carManagement.setRidgepole(ridgepole);
        carManagement.setRoomNumber(param.getRoomNumber());
        carManagement.setRidRoom(roomId);
        carManagement.setLicensePlateNumber(licensePlateNumber);
        carManagementMapper.insert(carManagement);


    }

    @Override
    public void editLicensePlateNumber(EditLicensePlateNumberParam param) {
        CarUser carUser = carUserMapper.selectById(param.getUserId());
        if(carUser.getStatus() != 1){
            throw new YamiShopBindException("您还没通过业主认证");
        }
        String licensePlateNumber = param.getLicensePlateNumber();

        CarManagement carManagement = carManagementMapper.selectById(param.getId());

        if(ObjectUtil.isNull(carManagement)){
            throw new YamiShopBindException("该车牌号不存在");
        }
        if(!carManagement.getRidRoom().equals(carUser.getRoomId())){
            throw new YamiShopBindException("该车牌号不属于您");
        }
        carManagementMapper.updateLicensePlateNumber(param.getId(),carManagement.getRidRoom(),licensePlateNumber);
    }
}
